﻿Public Class frmCaseCategory

    Private Sub frmCaseCategory_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        getHeaderGrid_CaseCategory()
        getCaseCategory()
        reset()
    End Sub

    Protected Sub getHeaderGrid_CaseCategory()
        Dim HeaderText As String() = {"លេខរៀង", "ID", "ឈ្មោះស្ថាប័នតុលាការ", "ពណ៌នា"}
        Dim HeaderWidth As Integer() = {100, 100, 700, 500}
        Dim i As Integer
        dgvData.ColumnCount = HeaderText.Count
        For i = 0 To HeaderText.Count - 1
            dgvData.Columns(i).HeaderText = HeaderText(i)
            dgvData.Columns(i).Width = HeaderWidth(i)
        Next
        dgvData.Columns(1).Visible = False
    End Sub

    Protected Sub getCaseCategory()
        dgvData.Rows.Clear()
        Dim oCaseCategory As New cCaseCategory
        Dim dt As New DataTable
        Dim dr As DataRow
        Dim i As Integer = 1

        dt = oCaseCategory.select_CaseCategory()
        For Each dr In dt.Rows
            dgvData.Rows.Add(i, dr(0), dr(1), dr(2))
            i += 1
        Next
    End Sub

    Protected Sub reset()
        txtCaseCategoryName.Clear()
        txtCaseCategoryName.Focus()
        txtDescription.Clear()
        txtSearch.Clear()
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Close()
    End Sub

    Private Sub btnClean_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClean.Click
        reset()
    End Sub

    Protected Function checknull() As Boolean
        Dim oc As New cControl
        If oc.checknull(txtCaseCategoryName.Text) Then
            Return True
        Else
            Return False
        End If
    End Function

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        inserted()
    End Sub

    Protected Sub inserted()
        Dim oCaseCategory As New cCaseCategory
        If checknull() Then
            If oCaseCategory.insert_CaseCategory(txtCaseCategoryName.Text, txtDescription.Text) Then
                MessageBox.Show("រក្សាទុកបានសម្រេច!", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Information)
                getCaseCategory()
                reset()
            Else
                MessageBox.Show("រក្សាទុកមិនបានសម្រេចទេ", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        Else
            MessageBox.Show("សូមបញ្ចូលគ្រប់ពត៌មានទាំងអស់!", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        If dgvData.Rows.Count > 0 Then
            If btnEdit.Text = "កែប្រែ" Then
                getTextbox()
            Else
                updated(dgvData.CurrentRow.Cells(1).Value)
            End If
        Else
            MessageBox.Show("គ្មានពត៌មានសម្រាប់់បង្ហាញទេ!", "បង្ហាញពត៌មាន", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

    Protected Sub updated(ByVal CaseCategoryID As Long)
        Dim oCaseCategory As New cCaseCategory
        If checknull() Then
            If oCaseCategory.update_CaseCategory(CaseCategoryID, txtCaseCategoryName.Text, txtDescription.Text) Then
                MessageBox.Show("កែប្រែបានសម្រេច!", "កែប្រែ", MessageBoxButtons.OK, MessageBoxIcon.Information)
                reset()
                getCaseCategory()
                btnEdit.Text = "កែប្រែ"
                enable(True, True, True, True)
            Else
                MessageBox.Show("កែប្រែមិនបានសម្រេចទេ", "កែប្រែ", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        Else
            MessageBox.Show("សូមបញ្ចូលគ្រប់ពត៌មានទាំងអស់!", "កែប្រែ", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

    Protected Sub getTextbox()
        Dim dt As New DataTable
        Dim oCaseCategory As New cCaseCategory
        dt = oCaseCategory.select_CaseCategory_byID(dgvData.CurrentRow.Cells(1).Value)
        Try
            txtCaseCategoryName.Text = dt.Rows(0)("CaseCategoryName")
            txtDescription.Text = dt.Rows(0)("Description")
            btnEdit.Text = "រក្សាទុក"
            enable(False, True, False, False)
        Catch ex As Exception

        End Try
    End Sub

    Protected Sub enable(ByVal Add As Boolean, ByVal Edit As Boolean, ByVal Delete As Boolean, ByVal Clean As Boolean)
        btnAdd.Enabled = Add
        btnEdit.Enabled = Edit
        btnDelete.Enabled = Delete
        btnClean.Enabled = Clean
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        deleted()
    End Sub

    Protected Sub deleted()
        Dim oCaseCategory As New cCaseCategory
        If dgvData.Rows.Count > 0 Then
            If MessageBox.Show("តើអ្នកប្រាកដជាលុប '" & dgvData.CurrentRow.Cells(2).Value & "' មែនទេ?", "លុបចោល", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                If oCaseCategory.delete_CaseCategory(dgvData.CurrentRow.Cells(1).Value) Then
                    MessageBox.Show("លុបចោលបានសម្រេច!", "លុបចោល", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    getCaseCategory()
                    reset()
                Else
                    MessageBox.Show("លុបចោលមិនបានសម្រេចទេ", "លុបចោល", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End If
            End If
        Else
            MessageBox.Show("គ្មានពត៌មានសម្រាប់់លុបទេ!", "លុបចោល", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

    Private Sub dgvData_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvData.DoubleClick
        btnEdit_Click(Nothing, Nothing)
    End Sub

    Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
        getSearch()
    End Sub

    Protected Sub getSearch()
        dgvData.Rows.Clear()
        Dim oCaseCategory As New cCaseCategory
        Dim dt As New DataTable
        Dim dr As DataRow
        Dim i As Integer = 1

        dt = oCaseCategory.select_CaseCategory_byKeyword(txtSearch.Text)
        For Each dr In dt.Rows
            dgvData.Rows.Add(i, dr(0), dr(1), dr(2))
            i += 1
        Next
    End Sub
End Class